package com.test.dababy.dababy_05_process;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.dababy.R;
import com.test.dababy.dababy_00_utils.DababyUtil;
import com.test.dababy.dababy_01_launcher.Base_dababyApplication;
import com.test.dababy.dababy_00_http.OkHttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class Acco_dababyuntCha_dababynnelViewAdapter extends RecyclerView.Adapter<Acco_dababyuntCha_dababynnelViewAdapter.AccountChannelHolder_dababy> {

    private final ArrayList<AccountCha_dababynn_dababyel_Fr> channels_dababy;

    private final String page_dababy;
    public String htd1="";
    public String aD1M="";
    public String j4mr="";
    public String zkXw="";
    public String g43B="";
    private double cn86=0.2520320228692672;
    private int xkMI=161;
    private String xIHR="";
    private String o95P="";
    private double w618=0.7519178163139255;

    public void s5Z8j(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getHtd1())) setaD1M(getJ4mr()+getCn86()+getW618());
                else setZkXw(getG43B()+getXkMI()+getO95P());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setCn86(double cn86) {
        this.cn86 = cn86;
    }

    public void setXkMI(int xkMI) {
        this.xkMI = xkMI;
    }

    public void setxIHR(String xIHR) {
        this.xIHR = xIHR;
    }

    public void setO95P(String o95P) {
        this.o95P = o95P;
    }

    public void setW618(double w618) {
        this.w618 = w618;
    }

    public String getHtd1() {
        return htd1;
    }

    public String getaD1M() {
        return aD1M;
    }

    public String getJ4mr() {
        return j4mr;
    }

    public String getZkXw() {
        return zkXw;
    }

    public String getG43B() {
        return g43B;
    }

    public double getCn86() {
        return cn86;
    }

    public int getXkMI() {
        return xkMI;
    }

    public String getxIHR() {
        return xIHR;
    }

    public String getO95P() {
        return o95P;
    }

    public double getW618() {
        return w618;
    }

    private AccountCha_dababynn_dababyel_Fr selectChannel_dababy;

    private final ArrayList<AccountChannelHolder_dababy> holderList_dababy = new ArrayList<>();

    public Acco_dababyuntCha_dababynnelViewAdapter(ArrayList<AccountCha_dababynn_dababyel_Fr> channels, String page){
        this.channels_dababy = channels;
        this.page_dababy = page;
    }

    public AccountCha_dababynn_dababyel_Fr getSelectChannel_dababy() {
        return selectChannel_dababy;
    }

    @NonNull
    @Override
    public AccountChannelHolder_dababy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AccountChannelHolder_dababy(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dababy_account_channel_fr,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull AccountChannelHolder_dababy holder, int position) {

        if (!holderList_dababy.contains(holder)) holderList_dababy.add(holder);

        AccountCha_dababynn_dababyel_Fr channel = channels_dababy.get(position);
        setZkXw("weroiike");

        holder.setChannelDesc_dababy(channel.getDesc_dababy());

        holder.setClick_dababy(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint(page_dababy,"DIALOG_TYPE_CLICK",channel.getDesc_dababy());
            selectChannel_dababy = channel;
            for (AccountChannelHolder_dababy holder1: holderList_dababy){
                holder1.setCheck_dababy(holder1 == holder);
            }
        });

        //获取图标
        if (channel.getBitmap_dababy()==null){
            new Thread(() -> {
                try {
                    setHtd1("Hgir");
                    if (TextUtils.isEmpty(channel.getLogo_dababy())) return;
                    URL url = new URL(channel.getLogo_dababy());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        channel.setBitmap_dababy(bitmap);
                        DababyUtil.safeRun(() -> holder.setLogo_dababy(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else holder.setLogo_dababy(channel.getBitmap_dababy());

    }

    @Override
    public int getItemCount() {
        s5Z8j();
        return channels_dababy.size();
    }

    public static class AccountChannelHolder_dababy extends RecyclerView.ViewHolder{

        private final View bgView;
        private final ImageView checkIv;
        private final ImageView logoIv;
        private final TextView channelTv;

        @SuppressLint("InflateParams")
        public AccountChannelHolder_dababy(View view){

            super(view);

            bgView = view.findViewById(R.id.iacfa_view_bg);
            checkIv = view.findViewById(R.id.iacfa_iv_check);
            logoIv = view.findViewById(R.id.iacfa_iv_logo);
            channelTv = view.findViewById(R.id.iacfa_tv_channel);

        }

        public void setClick_dababy(View.OnClickListener clickListener){
            if (bgView !=null) bgView.setOnClickListener(clickListener);
        }

        public void setCheck_dababy(boolean isCheck){
            if (isCheck){
                checkIv.setVisibility(View.VISIBLE);
                bgView.setBackgroundResource(R.drawable.bg_bank_channel_check);
                channelTv.setTextColor(Base_dababyApplication.getInstant().getColor(R.color.main));
            }else {
                checkIv.setVisibility(View.GONE);
                bgView.setBackgroundResource(R.drawable.bg_trans_ll_rc16dp);
                channelTv.setTextColor(Base_dababyApplication.getInstant().getColor(R.color.word_gray));
            }
        }

        public void setLogo_dababy(Bitmap bitmap){
            logoIv.setImageBitmap(bitmap);
        }

        public void setChannelDesc_dababy(String text){
            channelTv.setText(text);
        }

    }


    public void setHtd1(String htd1) {
        this.htd1 = htd1;
    }

    public void setaD1M(String aD1M) {
        this.aD1M = aD1M;
    }

    public void setJ4mr(String j4mr) {
        this.j4mr = j4mr;
    }

    public void setZkXw(String zkXw) {
        this.zkXw = zkXw;
    }

    public void setG43B(String g43B) {
        this.g43B = g43B;
    }

}
